package org.example.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;

@Component
public class PythonUtils {
    @Value("${xunfei.token}")
    private String APIpassword;

    public static String pythonScriptPath="src\\main\\resources\\python\\";

    public String readPythonFile(String fileName,String SetThings) {
        try {
            ProcessBuilder pb = new ProcessBuilder("python", pythonScriptPath+fileName, APIpassword, SetThings);
            pb.redirectErrorStream(true);
            pb.environment().put("PYTHONIOENCODING", "utf-8"); //设置环境变量
            Process p = pb.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), "UTF-8")); //设置输入流的编码
            StringBuilder output = new StringBuilder();
//            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
//            StringBuilder errorOutput = new StringBuilder();   //实际获取报错信息还是在p.getInputStream()

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line);
            }
            reader.close();
//            while ((line = stdError.readLine()) != null) {
//                errorOutput.append(line);
////                    System.out.println(line);
//            }
//            stdError.close();

            int exitCode = p.waitFor();
            if(exitCode==0) {
                return output.toString();
            } else {
                System.out.println(output);
                return null;
            }

//            if(exitCode==0) {
//                System.out.println("Python脚本执行成功");
//
//                //return JSON.parseArray(output.toString(), Map.class);
//                return output.toString();
//            } else {
//                System.out.println("Python脚本执行失败：exitCode=" + exitCode);
//                System.out.println(errorOutput);
//
//                //return Map.of("error", "Python脚本执行失败");
//                return null;
//            }
        } catch (Exception e) {
            System.out.println("Python脚本执行失败: " + e.getMessage());

//            return Map.of("error", "推荐失败: " + e.getMessage());
            return null;
        }
    }



//    public static boolean readPythonFile(String fileName) {
////        System.out.println("Start");
//        String projectPath = Paths.get("").toAbsolutePath().toString().replace("\\", "\\\\").replace("/", "\\\\");
////        String pyPath = projectPath + "\\src\\main\\resources\\python\\" + "recommend.py";
//        String pyPath = projectPath + "\\src\\main\\resources\\python\\" + fileName;
//
//        String[] args1 = new String[]{"python", pyPath, "111"};
//        try {
//            Process process = Runtime.getRuntime().exec(args1);
//            String line;
//
//            // 等待进程完成
////            0：表示子进程成功完成，没有错误。这是最常见的成功状态码。
////            1：一般性错误，表示程序执行失败。
////            2：命令行使用错误，通常表示参数不正确。
////            126：命令无法执行，可能是权限问题。
////            127：命令未找到，表示指定的命令不存在。
//            int exitCode = process.waitFor();
////            System.out.println("\nExited with code: " + exitCode);
//            if (exitCode == 0) {// 读取所有标准输出
//                BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
//                System.out.println("Standard output:");
//                while ((line = stdInput.readLine()) != null) {
//                    System.out.println(line);
//                }
//                stdInput.close();
////                return JSON.parseArray(output.toString(), Map.class);
//                return true;
//            } else {// 读取所有错误输出
//                BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
//                System.out.println("\nError output:");
//                while ((line = stdError.readLine()) != null) {
//                    System.out.println(line);
//                }
//                stdError.close();
////                return Map.of("error", "Python脚本执行失败");
//                return false;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
////            return Map.of("error", "推荐失败: " + e.getMessage());
//            return false;
//        } finally {
//
//        }
////        System.out.println("End");
//    }
}
